package com.turing.system.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.turing.system.entity.ParentCategory;
import com.turing.system.service.ParentCategoryService;
import constants.PageConstants;
import enums.ErrorCode;
import org.springframework.web.bind.annotation.*;
import result.CaseCadeInfo;
import result.Result;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 *  @author: shihonggang
 *  @Date: 2023/4/11 20:33
 *  @Description: 大类别控制器
 */
@RestController
@RequestMapping("parCategory")
public class ParCategoryController {

    @Resource
    private ParentCategoryService parentCategoryService;

    /**
     *  @author: shihonggang
     *  @Date: 2023/4/11 20:34
     *  @Description: 列表查询
     */
    @GetMapping("query")
    public Result query(
            @RequestParam(value = "pcName", required = false) String pcName,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ){
        LambdaQueryWrapper<ParentCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(pcName),ParentCategory::getPcName,pcName);
        PageHelper.startPage(pageNum, pageSize==null?PageConstants.PAGE_SIZE:pageSize);
        List<ParentCategory> list = parentCategoryService.list(wrapper);
        return Result.success(new PageInfo<>(list));
    }

    /**
     *  @author: shihonggang
     *  @Date: 2023/4/11 13:52
     *  @Description: 唯一性校验
     */
    @GetMapping("checkName")
    public Boolean checkName(
            @RequestParam("pcName") String pcName,
            @RequestParam(value = "pcId", required = false)String pcId
    ){
        LambdaQueryWrapper<ParentCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParentCategory::getPcName, pcName)
                .ne(StringUtils.isNotBlank(pcId), ParentCategory::getPcId, pcId);
        List<ParentCategory> list = parentCategoryService.list(wrapper);
        return list.size()==0?true:false;
    }

    /**
     *  @author: shihonggang
     *  @Date: 2023/4/11 20:32
     *  @Description: 增加保存
     */
    @PostMapping("addSave")
    public Result addSave(@RequestBody ParentCategory parentCategory){
        parentCategory.setPcId(UUID.randomUUID().toString());
        boolean rv = parentCategoryService.save(parentCategory);
        if (rv){
            return Result.success(parentCategory);
        }else {
            return Result.fail(ErrorCode.ERROR_ADD);
        }
    }

    /**
     *  @author: shihonggang
     *  @Date: 2023/4/11 20:33
     *  @Description: 根据主键查询
     */
    @GetMapping("getById")
    public Result getById(
            @RequestParam("id")String id
    ){
        ParentCategory pc = parentCategoryService.getById(id);
        return Result.success(pc);
    }

    /**
     *  @author: shihonggang
     *  @Date: 2023/4/11 20:36
     *  @Description: 修改保存
     */
    @PatchMapping("editSave")
    public Result editSave(@RequestBody ParentCategory parentCategory){
        boolean rv = parentCategoryService.updateById(parentCategory);
        if (rv){
            return Result.success(parentCategory);
        }else {
            return Result.fail(ErrorCode.ERROR_EDIT);
        }
    }

    @DeleteMapping("deleteByIds")
    public Result deleteByIds(
            @RequestParam("ids")String[] ids
    ){
        CaseCadeInfo infos = parentCategoryService.getCaseCadeInfoByIds(ids);
        if (infos != null){
            List<String> idsList = new ArrayList<>(Arrays.asList(ids));
            List<String> caseIds = new ArrayList<>(Arrays.asList(infos.getIds().split(",")));
            idsList.removeAll(caseIds);
            parentCategoryService.removeByIds(idsList);
            return Result.fail(12,infos.getNames()+" 删除失败,数据有关联");
        }else{
            boolean rv = parentCategoryService.removeByIds(Arrays.asList(ids));
            if (rv){
                return Result.success();
            }else {
                return Result.fail(ErrorCode.ERROR_DELETE);
            }
        }
    }
}
